﻿
big_picture_view = 
{
    dlg_ctrl_id_ = nil;
    dlg_ptr_ = nil;
    browser_ready_ = false;
    is_focus = false;
    is_delay_set_focus = false;

    url = LoadStr("BIGPIC_URL");

    init = function()
        big_picture_view.inner_log("init");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[big_picture][view] %s", msg), core_msg);
    end;

    open_big_picture_wnd = function()
        if big_picture_view.dlg_ctrl_id_ == nil then
            big_picture_view.dlg_ctrl_id_ = 
                                    layout_mgr.restore_layout("big_picture\\big_picture_dlg.tpf");
        end
        if big_picture_view.dlg_ptr_ == nil then
            big_picture_view.dlg_ptr_ = 
                                    ui_utility.get_ctrl_ptr_by_id(big_picture_view.dlg_ctrl_id_);
        end
        
        if big_picture_view.load_browser() == false then
            big_picture_view.show();
        end;
    end;

    close_big_picture_wnd = function()
        big_picture_view.OnDialogClose();
    end;

    can_bring_to_top = function()
        local data = comm_center.call_service("i_service_get_streaming_mode", {});
        if data == nil or data.mobile_streaming == nil or data.mobile_streaming == 0 then
            return true;
        end
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            if game_lib:IsAnyGameRunning() == true then
                return false;
            end;
        end;
        return true;
    end;

    show = function()
        --big_picture_view.show_mask(false);
        if big_picture_view.dlg_ptr_ ~= nil then
            big_picture_view.adjust_size();
            big_picture_view.dlg_ptr_:Show(TRUE);
            big_picture_view.delay_handle_bp_wnd_shown();

            if big_picture_view.can_bring_to_top() then
                big_picture_view.dlg_ptr_:BringToTop();
                local pBrowser = ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, "Ctn_WebArea\\QBWebBrowser");
                if pBrowser ~= nil then
                    pBrowser:SetFocus();
                end;
                big_picture_view.start_delay_set_focus(true);
            end;

            local softKeyBoard_id = ui_utility.get_ctrl_id_by_path(big_picture_view.dlg_ctrl_id_, "Ctn_SoftKeyboard");
            local pSoftKeyBoard = ui_utility.get_ctrl_ptr_by_id(softKeyBoard_id);
            if pSoftKeyBoard ~= nil then
                pSoftKeyBoard:FireJoyMessage(TRUE);
                pSoftKeyBoard:AttachJoyStickControlId(softKeyBoard_id);
                pSoftKeyBoard:EnableSoftkeyBoard(TRUE);
            end;      
        end;
    end;

    active_wnd = function()
        if big_picture_view.dlg_ptr_ ~= nil then
            big_picture_view.dlg_ptr_:BringToTop();
            local pBrowser = ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, "Ctn_WebArea\\QBWebBrowser");
            if pBrowser ~= nil then
                pBrowser:SetFocus();
            end;
        end;
    end;

    delay_handle_bp_wnd_shown = function()
        wegame_mainframe_view.close_main_window();
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui == nil then
            return;
        end
        tpfui:AddInterval('big_picture_view.handle_bp_wnd_shown', 500);
    end;

    handle_bp_wnd_shown = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            tpfui:RemoveInterval('big_picture_view.handle_bp_wnd_shown');
        end
        
        big_picture_control.on_bp_wnd_shown();
    end;

    is_big_pic_wnd_visible = function()
        local visible = false;
        if big_picture_view.dlg_ptr_ ~= nil then
            if big_picture_view.dlg_ptr_:IsVisible() == TRUE then
                visible = true;
            end;
        end;
        return visible;
    end;

    is_mask_visible = function()
        local visible = false;
        local mask_ptr = 
            ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, "mask");
        if mask_ptr ~= nil then
            if mask_ptr:IsVisible() == TRUE then
                visible = true;
            end;
        end;
        return visible;
    end;

    start_delay_set_focus = function(start)
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui == nil then
            return;
        end
        if start then
            if big_picture_view.is_delay_set_focus ~= true then
                big_picture_view.is_delay_set_focus = true;
                tpfui:AddInterval('big_picture_view.delay_set_focus', 2000);
            end
        else
            big_picture_view.is_delay_set_focus = false;
            tpfui:RemoveInterval('big_picture_view.delay_set_focus');
        end
    end;

    delay_set_focus = function()
        if big_picture_view.dlg_ctrl_id_ ~= nil then
            local pBrowser = ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, 
                                                             "Ctn_WebArea\\QBWebBrowser");
            if pBrowser ~= nil then
                pBrowser:SetFocus();
                big_picture_control.notify_bp_wnd_ready(true);
            end;
        end
        
        big_picture_view.start_delay_set_focus(false);
    end;

    adjust_size = function()
        if nil ~= big_picture_view.dlg_ptr_ then
            local rc = big_picture_view.dlg_ptr_:GetRectEx(TRUE, TRUE);
            local system_x = 0;
            local system_y = 0;
            system_x, system_y = big_picture_view.get_system_resolution();
            if nil == system_x or nil == system_y then
                return;
            end
            rc.left = 0;
            rc.right = system_x;
            rc.top = 0;
            rc.bottom = system_y;
            big_picture_view.dlg_ptr_:MoveEx(rc, TRUE);

            --[[local pBrowser = ui_utility.get_ctrl_ptr_by_path(
            big_picture_view.dlg_ctrl_id_, "Ctn_WebArea\\QBWebBrowser");
    
            if pBrowser ~= nil then
                pBrowser:MoveEx(rc, TRUE);
            end;--]]

        end;
    end;

    get_system_resolution = function()
        local tpf_ui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil == tpf_ui then
            return;
        end
        local sys_param = ierd_tgp.tpf_ui.SysParams:new();
        if nil == sys_param then
            return;
        end
        
        local x = 0;
        local y = 0;
        tpf_ui:get_sys_params(sys_param);
        x = sys_param.screen_width;
        y = sys_param.screen_height;
        sys_param:delete();
        return x, y;
    end;

    is_bp_wnd_ready = function()
        return big_picture_view.browser_ready_;
    end;

    OnDialogClose = function()
        if big_picture_view.dlg_ctrl_id_ ~= nil then
            layout_mgr.delay_remove_ctrl(big_picture_view.dlg_ctrl_id_);
            big_picture_view.dlg_ctrl_id_ = nil;
            big_picture_view.dlg_ptr_ = nil;
            big_picture_view.browser_ready_ = false;
        end;
        big_picture_control.notify_bp_wnd_ready(false);
    end;

    OnHide = function()
    end;

    OnDialogRestore = function()
        big_picture_view.start_delay_set_focus(true);
    end;

    navigate = function()
        local pBrowser = ui_utility.get_ctrl_ptr_by_path(
            big_picture_view.dlg_ctrl_id_, "Ctn_WebArea\\QBWebBrowser");
    
        if nil == pBrowser then
            return;
        end;
        local const_ui_str, str_holder = U8toCUIStr(big_picture_view.url);
        pBrowser:Navigate(const_ui_str);
    end;

    OnJsCallback = function(uilib, fuc_name, json, id)
    end;

    OnNavigateComplete = function()
        big_picture_view.show();
    end;

    OnNavigateFailed = function()
        big_picture_view.show();
    end;

    OnCrash = function()
        big_picture_view.show();
    end;

    -- 消息直接透传给qblink窗口
    notify_browser_msg = function(data)
        if big_picture_view.dlg_ctrl_id_ ~= nil then
            local pBrowser = ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, 
                                "Ctn_WebArea\\QBWebBrowser");
    
            if pBrowser ~= nil then
                --pBrowser:OnRecvExternalDeviceMsg(256,9,0);
                --pBrowser:OnRecvExternalDeviceMsg(257,9,0);
                --pBrowser:OnRecvExternalDeviceMsg(data.msg, data.wparam, data.lparam);
            end;
        end;
    end;

    ------------------------------------------处理焦点切换---------------------------------------
    OnBrowserSetFocus = function()
    end;

    OnBrowserKillFocus = function()
    end;

    OnDlgSetFocus = function()
    end;

    OnDlgKillFocus = function()
    end;

    load_browser = function()
        if big_picture_view.browser_ready_ == true then
            return false;
        end
        local browser_ctn_ptr = ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, 
                                                                    "Ctn_WebArea");
        if browser_ctn_ptr ~= nil then
            local is_load = browser_ctn_ptr:IsLoad();
            if is_load ~= TRUE then
                local result = browser_ctn_ptr:LoadContent();
                if result ~= TRUE then
                    local tpfui = base_utility.get_component_adapter("ITpf_ui",
                                                                    "ierd_tgp.tpf_ui.GetTPFUI();");
                    --tpfui:AddInterval('big_picture_view.delay_load_browser', 800);
                    return false;
                end
            end
            big_picture_view.navigate();
            big_picture_view.browser_ready_ = true;
            return true;
        end
        return false;
    end;

    unload_browser = function()
        big_picture_view.show_mask(true);
        if big_picture_view.browser_ready_ == false then
            return;
        end
        local browser_ctn_ptr = ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, 
                                                                "Ctn_WebArea");
        if browser_ctn_ptr ~= nil then
            browser_ctn_ptr:UnLoadContent();
            big_picture_view.browser_ready_ = false;
        end
    end;
    
    show_mask = function(show)
        local ret = false;
        local web_area_ptr = 
            ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, "Ctn_WebArea");  
        local mask_ptr = 
            ui_utility.get_ctrl_ptr_by_path(big_picture_view.dlg_ctrl_id_, "mask");
        if mask_ptr ~= nil and web_area_ptr ~= nil then
            if show then
                mask_ptr:Show(TRUE);
                web_area_ptr:Show(FALSE);
            else
                mask_ptr:Show(FALSE);
                web_area_ptr:Show(TRUE);
                local pBrowser = ui_utility.get_ctrl_ptr_by_path(
                        big_picture_view.dlg_ctrl_id_, "Ctn_WebArea\\QBWebBrowser");
                if pBrowser ~= nil then
                    pBrowser:SetFocus();
                end;
            end;
            ret = true;
        end;
        return ret;
    end;
    ---------------------------------------------------------------------------------

    mini_dlg = function()
        if nil ~= big_picture_view.dlg_ptr_ then
            big_picture_view.dlg_ptr_:Minimize();
        end
    end;

    exit_mode = function()
        big_picture_view.close_big_picture_wnd();
        local data = {};
        data.msg_type = "notify_exit_bp_ui_mode";
        comm_center.call_service("i_service_bp_ui_request", data);
    end;

    exit_wegame = function()
        --big_picture_view.close_big_picture_wnd();
        event_center.send_event(main_menue_event.MAIN_MENUE_EXIT_TGP, "");
    end;
};

big_picture_view.init();